package tn.talan.xdocreport.generationofreports;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.images.ClassPathImageProvider;
import fr.opensagres.xdocreport.document.images.IImageProvider;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;

import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import tn.talan.xdocreport.model.Project;
public class DocxProjectWithVelocityAndImage {

        public static void main(String[] args) {
                try {
                        // 1) Load Docx file by filling Velocity template engine and cache
                        // it to the registry
                        InputStream in = DocxProjectWithVelocityAndImage.class
                                        .getResourceAsStream("docxtemplatewithimages.docx");
                        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(
                                        in, TemplateEngineKind.Velocity);

                        // 2) Create fields metadata to manage image
                        FieldsMetadata metadata =  report.createFieldsMetadata();
                        metadata.addFieldAsImage("logo");
                       

                        // 3) Create context Java model
                        IContext context = report.createContext();
                        Project project = new Project("XDocReport");
                        context.put("project", project);
                        IImageProvider logo = new ClassPathImageProvider(
                                        DocxProjectWithVelocityAndImage.class, "logo.png");
                        context.put("logo", logo);

                        // 4) Generate report by merging Java model with the Docx
                        OutputStream out = new FileOutputStream(new File(
                                        "DocxProjectWithVelocityAndImage_Out.docx"));

                        report.process(context, out);
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (XDocReportException e) {
                        e.printStackTrace();
                }
        }
}